$(function () {
    // add listener to registerBtn
    $("#registerBtn").on("click", function () {
        //validate
        var validate = true;
        $("#registerForm").find("input[id!='password2']").each(function () {
            if (!$(this).val()) {
                var prompt = $(this);
                prompt.popover('show');
                setTimeout(function () {
                    prompt.popover('hide');
                }, 3000);
                validate = false;
            }
        });
        if ($("#password").val() != $("#password2").val()) {
            $("#password2").popover('show');
            setTimeout(function () {
                $("#password2").popover('hide');
            }, 3000);
            validate = false;
        }
        //pass
        if(validate){
             var formData = new FormData($("#registerForm").get(0));
            $.ajax({
                url: "/o2o-merchant/sec/rest/merchant",
                type: "post",
                data: formData,
                contentType: false,
                processData: false
            }).done(function (msg) {
                if (msg) {
                    window.location.href = "/o2o-merchant/login";
                } else {
                    $("#errorDiv").text("register fail");
                }
            });
        }

    });

    // add listener to backBtn
    $("#backBtn").on("click", function () {
        window.location.href = "/o2o-merchant/login";
    });
});